<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="说明安卓项目日渐稳定，遂学了一周的python（小菜鸡一个）。想找个项目，无奈网上的项目都不符合我实用的需求（看不懂…）。刚好最近安卓的项目频繁需要打包，于是就有了今天的找个标题，最后在不懈的努力（百度），只花了50行左右代码就搞定了。 代码写的很菜大家见谅就是发出来交流下，目前还没有实现多渠道分包和自动上传蒲公英或者fir，有需要大家可以自己补上。 准备工作 配置好gradle环境变量  你的项">
<meta property="og:type" content="article">
<meta property="og:title" content="Python实现gradle打包和360加固">
<meta property="og:url" content="http://yoursite.com/2021/01/16/python%E6%89%93%E5%8C%85apk/index.html">
<meta property="og:site_name" content="二松同学">
<meta property="og:description" content="说明安卓项目日渐稳定，遂学了一周的python（小菜鸡一个）。想找个项目，无奈网上的项目都不符合我实用的需求（看不懂…）。刚好最近安卓的项目频繁需要打包，于是就有了今天的找个标题，最后在不懈的努力（百度），只花了50行左右代码就搞定了。 代码写的很菜大家见谅就是发出来交流下，目前还没有实现多渠道分包和自动上传蒲公英或者fir，有需要大家可以自己补上。 准备工作 配置好gradle环境变量  你的项">
<meta property="og:image" content="https://i.loli.net/2021/01/16/1D82QCkpVxaZwW4.png">
<meta property="og:image" content="https://i.loli.net/2021/01/16/sjwltZx2J8yd3Fq.png">
<meta property="article:published_time" content="2021-01-16T05:50:35.000Z">
<meta property="article:modified_time" content="2021-01-16T05:53:03.424Z">
<meta property="article:author" content="二松">
<meta property="article:tag" content="android">
<meta property="article:tag" content="gradle">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/01/16/1D82QCkpVxaZwW4.png">

<link rel="canonical" href="http://yoursite.com/2021/01/16/python%E6%89%93%E5%8C%85apk/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-Hans'
  };
</script>

  <title>Python实现gradle打包和360加固 | 二松同学</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">二松同学</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">拥有梦想只是一种智力，实现梦想才是一种能力</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/01/16/python%E6%89%93%E5%8C%85apk/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
      <meta itemprop="name" content="二松">
      <meta itemprop="description" content="good good study day day up.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="二松同学">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Python实现gradle打包和360加固
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-01-16 13:50:35 / 修改时间：13:53:03" itemprop="dateCreated datePublished" datetime="2021-01-16T13:50:35+08:00">2021-01-16</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E5%8D%9A%E5%AE%A2/" itemprop="url" rel="index"><span itemprop="name">技术博客</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h2><p>安卓项目日渐稳定，遂学了一周的python（小菜鸡一个）。想找个项目，无奈网上的项目都不符合我实用的需求（看不懂…）。刚好最近安卓的项目频繁需要打包，于是就有了今天的找个标题，最后在不懈的努力（百度），只花了50行左右代码就搞定了。</p>
<p>代码写的很菜大家见谅就是发出来交流下，目前还没有实现多渠道分包和自动上传<code>蒲公英</code>或者<code>fir</code>，有需要大家可以自己补上。</p>
<h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><ol>
<li><p>配置好<code>gradle</code>环境变量</p>
<ul>
<li><p>你的项目<code>app</code>模块中的<code>gradle</code>要配置好打包签名</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">android &#123;</span><br><span class="line">    signingConfigs &#123;</span><br><span class="line">        config &#123;</span><br><span class="line">           <span class="comment">//省略部分</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    buildTypes &#123;</span><br><span class="line">        debug &#123;</span><br><span class="line">           <span class="comment">//省略部分</span></span><br><span class="line">           signingConfig signingConfigs.config</span><br><span class="line">        &#125;</span><br><span class="line"> </span><br><span class="line">        release &#123;</span><br><span class="line">          <span class="comment">//省略部分</span></span><br><span class="line">          signingConfig signingConfigs.config</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
</li>
</ol>
<ul>
<li><p>把<code>grale的bin目录</code>加到到系统变量<code>path</code>中</p>
</li>
<li><p>打开<code>cmd</code>输入<code>gradle</code>测试是否成功</p>
<p><img src="https://i.loli.net/2021/01/16/1D82QCkpVxaZwW4.png" alt="image-20210116114437079"></p>
</li>
</ul>
<ol start="2">
<li><p>安装python</p>
<ul>
<li>安装<code>python</code>3.x.x版本</li>
<li>选一个你喜欢的编辑器</li>
</ul>
</li>
</ol>
<h2 id="脚本代码"><a href="#脚本代码" class="headerlink" title="脚本代码"></a>脚本代码</h2><ul>
<li><p>具体实现看代码，如有不行大家见谅（语言能力不行啊），主要的流程就是先利用<code>gradle</code>打包后用<code>360加固</code></p>
</li>
<li><p><a href="https://jiagu.360.cn/#/global/help/164" target="_blank" rel="noopener">360加固打包命令官方地址</a>大家有需要可以看看</p>
</li>
<li><p><code>gradle</code>命令代码里有大家可以百度</p>
</li>
<li><p>成功截图</p>
<p><img src="https://i.loli.net/2021/01/16/sjwltZx2J8yd3Fq.png" alt="image-20210116120054883"></p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> argparse</span><br><span class="line"></span><br><span class="line"><span class="comment"># 加固路径</span></span><br><span class="line">jgPath = <span class="string">'D:\\360jiagubao_windows\jiagu\\'</span> <span class="comment">#改成你的3650加固文件夹地址</span></span><br><span class="line">output_path = <span class="string">'C:\\Users\\Desktop'</span> <span class="comment">#改成你要输出的目录</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取命令行的参数</span></span><br><span class="line">parse = argparse.ArgumentParser(description=<span class="string">'请输入android项目全路径'</span>)</span><br><span class="line">parse.add_argument(<span class="string">'input'</span>, type=str, help=<span class="string">'需要自动化的项目路径地址'</span>)</span><br><span class="line">args = parse.parse_args()</span><br><span class="line">input_path = args.input</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 查找当前目录apk文件</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_apk_path</span><span class="params">(path)</span>:</span></span><br><span class="line">    dirs = os.listdir(path)</span><br><span class="line">    <span class="keyword">for</span> f <span class="keyword">in</span> dirs:</span><br><span class="line">        print(<span class="string">'遍历文件:'</span>, f)</span><br><span class="line">        <span class="keyword">if</span> f.endswith(<span class="string">'.apk'</span>):</span><br><span class="line">            <span class="keyword">return</span> f</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动加固</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">auto_360_jg</span><span class="params">()</span>:</span></span><br><span class="line">    print(<span class="string">'开始编译文件'</span>)</span><br><span class="line">    <span class="comment"># 进入项目地址</span></span><br><span class="line">    os.chdir(input_path)</span><br><span class="line">    <span class="comment"># 执行编译命令</span></span><br><span class="line">    r = os.system(<span class="string">'gradle assembleRelease'</span>)</span><br><span class="line">    <span class="keyword">if</span> r == <span class="number">0</span>:</span><br><span class="line">        <span class="comment"># 安装包地址</span></span><br><span class="line">        build_path = input_path + <span class="string">'\\app\\build\\outputs\\apk\\release'</span></span><br><span class="line">        apk_path = get_apk_path(build_path)</span><br><span class="line">        <span class="keyword">if</span> apk_path:</span><br><span class="line">            print(<span class="string">'开始加固文件'</span>)</span><br><span class="line">            <span class="comment"># 进入加固目录</span></span><br><span class="line">            os.chdir(jgPath)</span><br><span class="line">            <span class="comment"># 执行加固命令</span></span><br><span class="line">            os.system(<span class="string">'java -jar jiagu.jar -showconfig'</span>)</span><br><span class="line">            cmd = <span class="string">'java -jar jiagu.jar -jiagu '</span> + build_path + <span class="string">'\\'</span> + apk_path + <span class="string">' '</span> + output_path + <span class="string">' -autosign'</span></span><br><span class="line">            os.system(cmd)</span><br><span class="line">            print(<span class="string">'恭喜,加固apk放置到桌面'</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(<span class="string">'哦哦,没有找到apk文件'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print(<span class="string">'哦哦,编译apk失败'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> input_path:</span><br><span class="line">    auto_360_jg()</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="string">'请输入apk地址'</span>)</span><br></pre></td></tr></table></figure>

<h2 id="运行脚本"><a href="#运行脚本" class="headerlink" title="运行脚本"></a>运行脚本</h2><ol>
<li><p>打开<code>cmd</code></p>
</li>
<li><p>输入<code>python xxx.py [项目目录]</code></p>
<p>例如<code>python test.py D:\AndroidProject\Example</code></p>
</li>
<li><p>想用<code>exe</code>可以自行打包</p>
</li>
</ol>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/android/" rel="tag"># android</a>
              <a href="/blog/tags/gradle/" rel="tag"># gradle</a>
              <a href="/blog/tags/python/" rel="tag"># python</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/09/26/linux%E7%8E%AF%E5%A2%83%E5%92%8C%E5%91%BD%E4%BB%A4/" rel="prev" title="linux环境和命令">
      <i class="fa fa-chevron-left"></i> linux环境和命令
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#说明"><span class="nav-number">1.</span> <span class="nav-text">说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#准备工作"><span class="nav-number">2.</span> <span class="nav-text">准备工作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#脚本代码"><span class="nav-number">3.</span> <span class="nav-text">脚本代码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运行脚本"><span class="nav-number">4.</span> <span class="nav-text">运行脚本</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="二松"
      src="/blog/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">二松</p>
  <div class="site-description" itemprop="description">good good study day day up.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">27</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">二松</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  













<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
